package com.cs.widgetvp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cs.base.util.FileDataUtil;
import com.cs.base.util.LogCat;
import com.cs.widgetvp.R;
import com.cs.widgetvp.cfg.VPConfig;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 *
 */
public class HomePictureVpAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private Context context;
    public HomePictureVpAdapter(Context context,List<String> pictures) {
        super(R.layout.home_picture_vp_item,pictures);
        this.context=context;
    }

    @Override
    protected void convert(@NotNull final BaseViewHolder holder, String url) {
        if(TextUtils.isEmpty(url)){
            Bitmap bitmap = FileDataUtil.getBitmapFromApp(VPConfig.PICTURE_FOLDER,"slide"+holder.getLayoutPosition());
            if(bitmap!=null){
                holder.setImageBitmap(R.id.iv_picture,bitmap);
            }else {
                if(holder.getLayoutPosition()==0){
                    holder.setImageResource(R.id.iv_picture,R.drawable.slide_1);
                }else if(holder.getLayoutPosition()==1){
                    holder.setImageResource(R.id.iv_picture,R.drawable.slide_2);
                }else if(holder.getLayoutPosition()==2){
                    holder.setImageResource(R.id.iv_picture,R.drawable.slide_3);
                }
            }
        }else {
            //Glide.with(context).load(pictures.get(position)).into(holder.iv_picture);
            Glide.with(context).load(url).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    holder.setImageDrawable(R.id.iv_picture,resource);
                    BitmapDrawable bd = (BitmapDrawable) resource;
                    Bitmap bm = bd.getBitmap();
                    FileDataUtil.saveBitmapToApp(bm, VPConfig.PICTURE_FOLDER,"slide"+holder.getLayoutPosition());
                }
            });
        }
        LogCat.i("HomePictureVpAdapter","getOldPosition="+holder.getLayoutPosition());
    }

}
